/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import test.entities.Client;
import test.entities.Action;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import test.controllers.exceptions.NonexistentEntityException;
import test.controllers.exceptions.PreexistingEntityException;
import test.entities.OrdreVente;

/**
 *
 * @author Issam
 */
public class OrdreVenteJpaController implements Serializable {

    public OrdreVenteJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OrdreVente ordreVente) throws PreexistingEntityException, Exception {
        if (ordreVente.getActionCollection() == null) {
            ordreVente.setActionCollection(new ArrayList<Action>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client email = ordreVente.getEmail();
            if (email != null) {
                email = em.getReference(email.getClass(), email.getEmail());
                ordreVente.setEmail(email);
            }
            Collection<Action> attachedActionCollection = new ArrayList<Action>();
            for (Action actionCollectionActionToAttach : ordreVente.getActionCollection()) {
                actionCollectionActionToAttach = em.getReference(actionCollectionActionToAttach.getClass(), actionCollectionActionToAttach.getNumAction());
                attachedActionCollection.add(actionCollectionActionToAttach);
            }
            ordreVente.setActionCollection(attachedActionCollection);
            em.persist(ordreVente);
            if (email != null) {
                email.getOrdreVenteCollection().add(ordreVente);
                email = em.merge(email);
            }
            for (Action actionCollectionAction : ordreVente.getActionCollection()) {
                actionCollectionAction.getOrdreVenteCollection().add(ordreVente);
                actionCollectionAction = em.merge(actionCollectionAction);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findOrdreVente(ordreVente.getNumVente()) != null) {
                throw new PreexistingEntityException("OrdreVente " + ordreVente + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OrdreVente ordreVente) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            OrdreVente persistentOrdreVente = em.find(OrdreVente.class, ordreVente.getNumVente());
            Client emailOld = persistentOrdreVente.getEmail();
            Client emailNew = ordreVente.getEmail();
            Collection<Action> actionCollectionOld = persistentOrdreVente.getActionCollection();
            Collection<Action> actionCollectionNew = ordreVente.getActionCollection();
            if (emailNew != null) {
                emailNew = em.getReference(emailNew.getClass(), emailNew.getEmail());
                ordreVente.setEmail(emailNew);
            }
            Collection<Action> attachedActionCollectionNew = new ArrayList<Action>();
            for (Action actionCollectionNewActionToAttach : actionCollectionNew) {
                actionCollectionNewActionToAttach = em.getReference(actionCollectionNewActionToAttach.getClass(), actionCollectionNewActionToAttach.getNumAction());
                attachedActionCollectionNew.add(actionCollectionNewActionToAttach);
            }
            actionCollectionNew = attachedActionCollectionNew;
            ordreVente.setActionCollection(actionCollectionNew);
            ordreVente = em.merge(ordreVente);
            if (emailOld != null && !emailOld.equals(emailNew)) {
                emailOld.getOrdreVenteCollection().remove(ordreVente);
                emailOld = em.merge(emailOld);
            }
            if (emailNew != null && !emailNew.equals(emailOld)) {
                emailNew.getOrdreVenteCollection().add(ordreVente);
                emailNew = em.merge(emailNew);
            }
            for (Action actionCollectionOldAction : actionCollectionOld) {
                if (!actionCollectionNew.contains(actionCollectionOldAction)) {
                    actionCollectionOldAction.getOrdreVenteCollection().remove(ordreVente);
                    actionCollectionOldAction = em.merge(actionCollectionOldAction);
                }
            }
            for (Action actionCollectionNewAction : actionCollectionNew) {
                if (!actionCollectionOld.contains(actionCollectionNewAction)) {
                    actionCollectionNewAction.getOrdreVenteCollection().add(ordreVente);
                    actionCollectionNewAction = em.merge(actionCollectionNewAction);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = ordreVente.getNumVente();
                if (findOrdreVente(id) == null) {
                    throw new NonexistentEntityException("The ordreVente with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            OrdreVente ordreVente;
            try {
                ordreVente = em.getReference(OrdreVente.class, id);
                ordreVente.getNumVente();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ordreVente with id " + id + " no longer exists.", enfe);
            }
            Client email = ordreVente.getEmail();
            if (email != null) {
                email.getOrdreVenteCollection().remove(ordreVente);
                email = em.merge(email);
            }
            Collection<Action> actionCollection = ordreVente.getActionCollection();
            for (Action actionCollectionAction : actionCollection) {
                actionCollectionAction.getOrdreVenteCollection().remove(ordreVente);
                actionCollectionAction = em.merge(actionCollectionAction);
            }
            em.remove(ordreVente);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OrdreVente> findOrdreVenteEntities() {
        return findOrdreVenteEntities(true, -1, -1);
    }

    public List<OrdreVente> findOrdreVenteEntities(int maxResults, int firstResult) {
        return findOrdreVenteEntities(false, maxResults, firstResult);
    }

    private List<OrdreVente> findOrdreVenteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(OrdreVente.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OrdreVente findOrdreVente(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OrdreVente.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrdreVenteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<OrdreVente> rt = cq.from(OrdreVente.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
